export class ServerSideDatasource {

  private fakeServer:any;
  constructor(private fake:FakeServer){
    this.fakeServer = fake;
  }
  getRows(params){
    let rows = this.fakeServer.getData(params.request);
    setTimeout(() => {
      params.successCallback(rows, rows.length);
    }, 200);
  }

}

export class FakeServer{
  private data:any;
  constructor(private allData:any){
    this.data = allData;
  }
  getData(params){
    return this.extractRowsFromData(params.groupKeys, this.data)
  }

  extractRowsFromData(groupKeys, data) {
    if (groupKeys.length === 0) {
      return data.map(d => {
        let item ={};
        for (let key in d){
          if (key !=='children'){
            item[key]=d[key];
          }
        }
        item['group'] = !!d.children;
        item['name'] = d.name;
        return item;
      });
    }

    var key = groupKeys[0];
    for (var i = 0; i < data.length; i++) {
      if (data[i].id === key) {
        return this.extractRowsFromData(groupKeys.slice(1), data[i].children.slice());
      }
    }
  }
}
